package com.aquarium.alias;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import android.widget.ArrayAdapter;

import com.aquarium.R;
import com.aquarium.config.BTCommand;
import com.aquarium.util.SPUtils;

public class AliasManager {
	private static final String TAG = "SecurityManager";

	private static final String PREFER_NAME = "alias";
	private static final String CONTROLLER1_KEY = "controller1";
	private static final String CONTROLLER2_KEY = "controller2";
	private static final String CONTROLLER3_KEY = "controller3";
	private static final String CONTROLLER4_KEY = "controller4";
	private static final String CONTROLLER5_KEY = "controller5";
	private static final String CONTROLLER6_KEY = "controller6";

	private static Context mContext;
	private SharedPreferences mAliasPreferences;
	private static AliasManager mAliasManager;
	private static ArrayAdapter<CharSequence> mArrayAdapter;

	private AliasManager() {
		mAliasPreferences = mContext.getSharedPreferences(PREFER_NAME,
				Context.MODE_PRIVATE);
		mArrayAdapter = ArrayAdapter
				.createFromResource(mContext, R.array.controllers_array,
						android.R.layout.simple_spinner_item);
	}

	public static AliasManager getInstance(Context context) {
		mContext = context;
		if (mAliasManager == null) {
			mAliasManager = new AliasManager();
		}
		return mAliasManager;
	}

	public void setSelectedId(int[] mSelectedId) {
		if (mSelectedId.length != 6) {
			Log.e(TAG, "length error");
			return;
		}
		SPUtils.setSP(mAliasPreferences.edit(), CONTROLLER1_KEY, mSelectedId[0]);
		SPUtils.setSP(mAliasPreferences.edit(), CONTROLLER2_KEY, mSelectedId[1]);
		SPUtils.setSP(mAliasPreferences.edit(), CONTROLLER3_KEY, mSelectedId[2]);
		SPUtils.setSP(mAliasPreferences.edit(), CONTROLLER4_KEY, mSelectedId[3]);
		SPUtils.setSP(mAliasPreferences.edit(), CONTROLLER5_KEY, mSelectedId[4]);
		SPUtils.setSP(mAliasPreferences.edit(), CONTROLLER6_KEY, mSelectedId[5]);
	}

	public int[] getControllerType() {
		int[] mSelectedId = new int[6];
		// if (mSelectedId.length != 6) {
		// Log.e(TAG, "length error");
		// return;
		// }
		mSelectedId[0] = SPUtils.getSP(mAliasPreferences, CONTROLLER1_KEY, 0);
		mSelectedId[1] = SPUtils.getSP(mAliasPreferences, CONTROLLER2_KEY, 0);
		mSelectedId[2] = SPUtils.getSP(mAliasPreferences, CONTROLLER3_KEY, 0);
		mSelectedId[3] = SPUtils.getSP(mAliasPreferences, CONTROLLER4_KEY, 0);
		mSelectedId[4] = SPUtils.getSP(mAliasPreferences, CONTROLLER5_KEY, 0);
		mSelectedId[5] = SPUtils.getSP(mAliasPreferences, CONTROLLER6_KEY, 0);
		return mSelectedId;
	}

	/**
	 * get controller position through name.
	 * 
	 * @param item
	 *            controller name
	 * @return the position of controller, -1 means "not found".
	 */
	public int getControllerPosition(String item) {

		int position = mArrayAdapter.getPosition(item);
		if (position < 0) {
			return -1;
		}
		int[] mSelectedId = getControllerType();
		int length = mSelectedId.length;
		for (int i = 0; i < length; i++) {
			if (position == mSelectedId[i]) {
				return i + 1;
			}
		}

		return -1;
	}

}
